import { createRouter, RouteRecordRaw, createWebHashHistory } from 'vue-router'
import AppLayout from '../layout/AppLayout.vue'
import productRouter from './modules/product'
import nprogress from 'nprogress'
import 'nprogress/nprogress.css'
import { store } from '@/store'

import settingRouter from './modules/setting'
// import systemRouter from './modules/system'

const routes: RouteRecordRaw[] = [
    {
        path: '/',
        component: AppLayout,
        meta: { title: '', requiresAuth: true },
        children: [
            {
                path: '',
                name: 'home',
                component: () => import('../views/home/homePage.vue'),
                meta: { title: '首页' }
            },
            productRouter,
            settingRouter,
            // systemRouter
        ]
    },
    {
        path: '/login',
        component: () => import('../views/login/loginPage.vue')
    }
]

const router = createRouter({
    history: createWebHashHistory(),
    routes
})
router.beforeEach((to) => {
    nprogress.start()
      // 而不是去检查每条路由记录
  // to.matched.some(record => record.meta.requiresAuth)
  if (to.meta.requiresAuth && !store.state.user) {
    // 此路由需要授权，请检查是否已登录
    // 如果没有，则重定向到登录页面
    return {
      path: '/login',
      // 保存我们所在的位置，以便以后再来
      query: { redirect: to.fullPath },
    }
  }
})
router.afterEach(() => {
    nprogress.done()
})

export default router